package com.yaoxuguang.concurrence.threadBase.async_thread;

import java.util.concurrent.LinkedBlockingQueue;

/**
 * @author yaoxuguang
 * @Title: yao-interview
 * @Package com.yaoxuguang.concurrence.threadBase.async_thread
 * @Description: 打印处理
 * @date 2018年12月14日14:54
 */
public class PrintProcessor extends Thread implements RequestProcessor{
    LinkedBlockingQueue<Request> requests = new LinkedBlockingQueue<>();

    private final RequestProcessor nextProcessor;

    public PrintProcessor(RequestProcessor nextProcessor){
        this.nextProcessor = nextProcessor;
    }

    @Override
    public void run() {
        while (true){
            try {
                Request request = requests.take();
                System.out.println("print data:"+request.getName());
                nextProcessor.processRequest(request);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void processRequest(Request request) {
        requests.add(request);
    }
}
